﻿using QldtSdh.GUI.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using QldtSdh.DAL.Models;

namespace QldtSdh.GUI
{
    public partial class FormChonHocKyTacNghiep : BaseForm
    {
        public DAL.Models.HocKy SelectedHocKy { get; private set; }

        public FormChonHocKyTacNghiep(HocKy selectedHocKy)
        {
            InitializeComponent();

            NapDanhSachHocKy(selectedHocKy == null ? null : selectedHocKy.MaHocKyGUID);
        }

        /// <summary>
        /// Nạp danh sách học kỳ, sau đó cho chọn tại học kỳ có mã học kỳ tương ứng
        /// </summary>
        /// <param name="maHocKyToFocus"></param>
        private void NapDanhSachHocKy(string maHocKyToFocus = null)
        {
            var lst = BLL.BizHocKy.GetListHocKy(conHoatDong: chkChiHienHKConHoatDong.Checked);
            bsHocKy.DataSource = lst;
            if (maHocKyToFocus == null)
            {
                if (bsHocKy.Count > 0)
                    bsHocKy.Position = 0;
            }
            else
            {
                for (int i = 0; i < lst.Count; i++)
                    if (lst[i].MaHocKyGUID == maHocKyToFocus)
                    {
                        bsHocKy.Position = i;
                        break;
                    }
            }
        }

        private void btnOK_Click(object sender, EventArgs e)
        {
            HocKy hk = null;
            if (bsHocKy.Current != null)
                hk = bsHocKy.Current as HocKy;
            if (hk != null)
            {
                this.SelectedHocKy = hk;
                this.DialogResult = System.Windows.Forms.DialogResult.OK;
                this.Close();
            }
        }

        private void chkChiHienHKConHoatDong_CheckedChanged(object sender, EventArgs e)
        {
            NapDanhSachHocKy();
        }

        private void gridData_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
        {
            btnOK.PerformClick();
        }
    }
}
